/**
 * Created by snake on 2015/9/2.
 */
//AngularJS 控制器
grApp.controller('ClassifyCtrl',['$scope',
    'BaseService',
    'toaster',
    'ClassifyProductFactory',
    function($scope,BaseService,toaster,ClassifyProductFactory){
        $scope.ClassifyProductArray = {};//分类商品列表
        $scope.nowPage = 1;
        $scope.sort = "xp";
        $scope.getMore = true;

        //页面初始化载入顺序事件
        $scope.searchPro=function(){
            if($scope.name == "" || null == $scope.name){
                alert("请输入商品名称!");
            }else{
                location.href="/classify/search/"+$scope.name;
            }
        }
        $scope.addToCart = function(pid,sid,num,pname){
            if (pname.indexOf('预售')>0 || pname.indexOf('福利')>0 || pname.indexOf('抢购')>0){
                if (pname.indexOf('预售')>0){
                    alert('预售商品不可加入购物车');
                }else if(pname.indexOf('福利')>0){
                    alert('福利商品不可加入购物车');
                }else if(pname.indexOf('抢购')>0){
                    alert('抢购商品不可加入购物车');
                }else{
                    alert('活动商品不可加入购物车');
                }
            }else{
                BaseService.addToCart(pid,sid,num).then(function(res){
                    if(res.code==259){
                        if(res.reason.code==-1){
                            toaster.error('库存不足');
                        }
                    }else{
                        toaster.success('添加成功');
                    }
                }).catch(function(err){
                    toaster.error('添加失败');
                });
            }
        }

        $scope.getProsLimit = function(){
            ClassifyProductFactory.getProductList($scope.nowPage,$scope.sort).then(function(res){
                    $scope.ClassifyProductArray = ClassifyProductFactory.getProductArray();
                    var proCount = ClassifyProductFactory.getProductArray().length;
                    if(proCount<10){
                        $scope.getMore = false;
                    }
                    $scope.nowPage++;
            }).catch(function(err){

            })
        }

        $scope.getMoreProsLimit = function(){
            ClassifyProductFactory.getProductList($scope.nowPage,$scope.sort).then(function(res){
                var productArray = ClassifyProductFactory.getProductArray();
                var proCount = ClassifyProductFactory.getProductArray().length;
                if(proCount<10){
                    $scope.getMore = false;
                }else{
                    for(var index in productArray){
                        $scope.ClassifyProductArray.push(productArray[index]);
                    }
                }
                $scope.nowPage++;
            }).catch(function(err){

            })
        }

        $scope.sortStr = function(key){
            $scope.nowPage = 1;
            $scope.ClassifyProductArray = {};
            $scope.sort = key;
            $scope.getMore = true;

            ClassifyProductFactory.getProductList($scope.nowPage,$scope.sort).then(function(res){
                $scope.ClassifyProductArray = ClassifyProductFactory.getProductArray();
                var proCount = ClassifyProductFactory.getProductArray().length;
                if(proCount<10){
                    $scope.getMore = false;
                }
                $scope.nowPage++;
            }).catch(function(err){

            })
        }

        $scope.sortStrOnly = function(key){
            $scope.sort = key;
        }

        $scope.getProsLimit();
}]);

//获取商品数据
grApp.factory('ClassifyProductFactory',['$http','$q',function($http,$q){
    var _productArray; //数据数组
    var service = {};

    //获取分类商品数据
    service.getProductList = function (page,sort) {
        var deferred = $q.defer(); // 声明延后执行，表示要去监控后面的执行
        $http({
            method: 'GET',
            url: '/classify/getClassifyProductListAjax',
            params: {page: page,sort:sort},
            //url: '/cart/getProductListAjax',
        }).success(function (data, status, headers, config) {
            _productArray = data.ProductArray;
            deferred.resolve(data.ProductArray);
        }).error(function (data, status, headers, config) {
            deferred.reject(data);
        });
        return deferred.promise;
    };

    //获取数据数组
    service.getProductArray = function () {
        return _productArray;
    }
    return service;
}]);